package ${package.Controller};

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gomcarter.frameworks.base.common.AssertUtils;
import com.gomcarter.frameworks.base.pager.DefaultPager;
import ${custom_package_dto}.${custom_class_Dto};
import ${package.Entity}.${entity};
import ${custom_package_params}.${custom_class_CreateParam};
import ${custom_package_params}.${custom_class_QueryParam};
import ${custom_package_params}.${custom_class_UpdateParam};
import ${package.Service}.${table.serviceName};
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

<#if superControllerClassPackage??>
import ${superControllerClassPackage};
</#if>
import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.stream.Collectors;

import static com.yifeng.repo.micro.service.server.engine.core.LambdaTransformer.*;

/**
 * <p>
 * ${table.comment!} 前端控制器
 * </p>
 *
 * @author ${author}
 * @since ${date}
 */
@Slf4j
@Validated
<#if restControllerStyle>
@RestController
<#else>
@Controller
</#if>
@RequestMapping("${custom_request_mapping}")
<#if kotlin>
class ${table.controllerName}<#if superControllerClass??> : ${superControllerClass}()</#if>
<#else>
<#if superControllerClass??>
public class ${table.controllerName} extends ${superControllerClass} {
<#else>
public class ${table.controllerName} {
</#if>

    @Resource
    private ${table.serviceName} ${custom_instance_name};

    /**
     * 主键查询：${table.comment!}
     * @param ${custom_idName} ${custom_idComment}
     * @return ${table.comment!}对象
     */
    @GetMapping(value = "get")
    public ${custom_class_Dto} get(@NotNull(message = "${custom_idName}不能为空") ${custom_idSimpleType} ${custom_idName}) {
        ${entity} entity = this.${custom_instance_name}.getById(${custom_idName});
        return copyProperties(entity, new ${custom_class_Dto}());
    }

    /**
     * 列表查询：${table.comment!}
     * @param params 查询条件
     * @param pager  分页条件
     * @return ${table.comment!}列表
     */
    @GetMapping(value = "list")
    public List<${custom_class_Dto}> list(${custom_class_QueryParam} params, DefaultPager pager) {
        LambdaQueryWrapper<${entity}> lambdaWrapper = buildQueryWrapper(${entity}.class, params, pager);
        return this.${custom_instance_name}.list(lambdaWrapper)
                .stream()
                .map(s -> copyProperties(s, new ${custom_class_Dto}()))
                .collect(Collectors.toList());
    }

    /**
     * 查询总数：${table.comment!}
     * @param params 查询条件
     * @return 总记录数
     */
    @GetMapping(value = "count")
    public Long count(${custom_class_QueryParam} params) {
        LambdaQueryWrapper<${entity}> lambdaWrapper = buildCountWrapper(${entity}.class, params);
        return this.${custom_instance_name}.count(lambdaWrapper);
    }

    /**
     * 新增数据：${table.comment!}
     * @param params 新增条件
     * @return ${custom_idComment}
     */
    @PostMapping(value = "create")
    public ${custom_idSimpleType} create(@RequestBody ${custom_class_CreateParam} params) {
        ${entity} entity = copyProperties(params, new ${entity}());
        this.${custom_instance_name}.save(entity);
        return entity.${custom_idGetMethod}();
    }

    /**
     * 修改数据：${table.comment!}
     * @param params 修改条件
     * @return 是否修改成功
     */
    @PostMapping(value = "update")
    public boolean update(@RequestBody ${custom_class_UpdateParam} params) {
        ${entity} entity = this.${custom_instance_name}.getById(params.${custom_idGetMethod}());
        AssertUtils.notNull(entity);
        return this.${custom_instance_name}.updateById(copyProperties(params, entity));
    }

    /**
     * 删除数据：${table.comment!}
     * @param ${custom_idName} ${custom_idComment}
     * @return 是否删除成功
     */
    @PostMapping(value = "delete")
    public boolean delete(@NotNull(message = "${custom_idName}不能为空") ${custom_idSimpleType} ${custom_idName}) {
        return this.${custom_instance_name}.removeById(${custom_idName});
    }
}
</#if>
